﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Serialization;

namespace PersonaLibrary.CustomProperties
{
	[XmlRoot("item")]
	public class PropertiesItem : INotifyPropertyChanged
	{
		private bool _isAlphabetFilter, _isAllowRegister;
		private string _alphabetFilterProperty, _alphabetFilterValue;
		private Alphabet _alphabetLanguage;

		public PropertiesItem()
		{
			Alphabets = new List<Alphabet> { new RuAlphabet(), new EnAlphabet() };
		}

		[XmlElement]
		public bool IsAlphabetFilter
		{
			get
			{
				return _isAlphabetFilter;
			}
			set
			{
				_isAlphabetFilter = value;
				RisePropertyChanged("IsAlphabetFilter");
				NeedRefresh();
			}
		}

		[XmlElement]
		public bool IsAllowRegister
		{ 
			get
			{
				return _isAllowRegister;
			}
			set
			{
				_isAllowRegister = value;
				RisePropertyChanged("IsAllowRegister");
				NeedRefresh();
			}
		}

		[XmlElement]
		public string AlphabetFilterProperty
		{
			get
			{
				return _alphabetFilterProperty;
			}
			set
			{
				_alphabetFilterProperty = value;
				RisePropertyChanged("AlphabetFilterProperty");
				NeedRefresh();
			}
		}

		[XmlElement]
		public string AlphabetFilterValue
		{
			get
			{
				return _alphabetFilterValue;
			}
			set
			{
				_alphabetFilterValue = value;
				RisePropertyChanged("AlphabetFilterValue");
				NeedRefresh();
			}
		}

		[XmlElement]
		public int ActiveAlphabetIndex
		{
			set
			{
				AlphabetLanguage = Alphabets[value];
			}
			get
			{
				return Alphabets.IndexOf(AlphabetLanguage);
			}
		}
		
		[XmlIgnore]
		public Alphabet AlphabetLanguage
		{
			get
			{
				return _alphabetLanguage;
			}
			set
			{
				_alphabetLanguage = value;
				RisePropertyChanged("AlphabetLanguage");
			}
		}

		[XmlIgnore]
		public List<Alphabet> Alphabets { private set; get; }
		
		public event PropertyChangedEventHandler PropertyChanged;

		public event Action NeedRefresh = delegate { };

		protected void RisePropertyChanged(string name)
		{
			if (PropertyChanged != null) 
				PropertyChanged(this, new PropertyChangedEventArgs(name));

			if (Properties.Settings.Default != null)
				Properties.Settings.Default.Save();
		}

		public void SetDefault()
		{
			_isAlphabetFilter = true;
			_isAllowRegister = false;
			_alphabetFilterProperty = "Name";
			_alphabetFilterValue = "a";
			AlphabetLanguage = Alphabets[0];
		}
	}
}
